<?php
namespace Particle\Apps;
use Particle\Core as Core;

final class essential {
    
    public static final function redirect($path = false, $external= false, $useJs = false, $objView = false) {
        /** @todo PHP redirect or JS redirect **/
        if ($path) {
            
            if($external){
                if($useJs){
                    $redirectJs = $path;
                }else{
                    header('location:' . $path);
                    exit;
                }
                
            }else{
                if($useJs){
                    $redirectJs = HOME_URL . $path;
                }else{
                    header('location:' . HOME_URL . $path);
                    exit;
                }
            }

        } else {
            if($useJs){
                   $redirectJs = HOME_URL;
            }else{
                header('location:' . HOME_URL);
                exit;
            }
            
        }
        
        if($useJs == true && is_object($objView)){ // TODO: Test!!
            $objView->assign($redirectJs);
            $objView->show('redirect');
            exit();
        }else{
            throw new \Exception('Error JS Redirect');
        }
        
    }
       
    public static final function getLibrary($library) {
        
        $pathLibrary = APP_PATH . 'libs' . DS . $library . '.php';

        if (is_readable($pathLibrary)) {
            require_once $pathLibrary;
        } else {
            throw new \Exception('Error Library');
        }
    }

    public static final function getText($key, $array = null, $default = '', $filterXSS= false) {
        
        if(is_null($array)){
            $array = $_REQUEST;
        }
        
        if (isset($array[$key]) && !empty($array[$key])) {
            
            if($filterXSS){
                $filterText = Core\Security::filterXSS($array[$key], $default);
            }else{
                $filterText = Core\Security::cleanHtml($array[$key], $default);
            }
            
            
            return $filterText;
        }

        return $default;
    }

    public static final function getInt($key, $array = null, $default = 0){
        
        if(is_null($array)){
            $array = $_POST;
        }
        
        if (isset($array[$key]) && !empty($array[$key])){
            
            if($array != $_SESSION){
                
                if($array == $_GET){
                    $input = INPUT_GET;
                }else if($array == $_POST){
                    $input = INPUT_POST;
                }else if($array == $_COOKIE){
                    $input = INPUT_COOKIE;
                }else{
                    return false;
                }

                $array[$key] = filter_input($input, $key, FILTER_VALIDATE_INT);
            }
            $filterInt = Core\Security::filterInt($array[$key], $default);
            
            return $filterInt;
        }

        return 0;
    }

    public static final function getParam($key, $array = null, $default = false){
        
        if(is_null($array)){
            $array = $_REQUEST;
        }
        
        if (isset($array[$key])) {
            return $array[$key];
        }else{
            return $default;
        }
    }

    public static final function getSql($key, $array = null, $html = true, $default = false){
        
        if(is_null($array)){
            $array = $_REQUEST;
        }
        if (isset($array[$key]) && !empty($array[$key])) {
            return Core\Security::filterSql($array[$key], $html, $default);
        }else{
            return $default;
        }
    }

    public static final function getAlphaNum($key, $array = null, $default = ''){
        
        if(is_null($array)){
            $array = $_REQUEST;
        }
        
        if (isset($array[$key]) && !empty($array[$key])) {
            return Core\Security::filterAlphaNum($array[$key], $default);
        }else{
            return $default;
        }
        
    }
    
    public static final function uploadFile($File, $FullPatch, $allowedExts = false, $allowedTypes = false){
        
        if(!$allowedExts){
            $allowedExts = array("jpg", "jpeg", "gif", "png", "zip");
        }
        
        if(!$allowedTypes){
             $allowedTypes = array("image/gif", "image/jpeg", "image/pjpeg");
        }
        
        $extension = end(explode(".", $File["name"]));
        // TODO: add $allowedTypes
        if (($File["size"] < 20000) && in_array($extension, $allowedExts)) {
          if ($File["error"] > 0) {
                echo "Return Code: " . $File["error"] . "<br />";
            } else {
                
                echo "Upload: " . $File["name"] . "<br />";
                echo "Type: " . $File["type"] . "<br />";
                echo "Size: " . ($File["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $File["tmp_name"] . "<br />";

                if (file_exists($FullPatch . $File["name"])) {
                    echo $File["name"] . " already exists. ";
                } else {
                    move_uploaded_file($File["tmp_name"], $FullPatch . $File["name"]);
                    echo "Stored in: " . $FullPatch . $File["name"];
                    return $FullPatch . $File["name"];
                }
            }
          } else {
            echo "Error - Invalid File";
          }
    }
    
}
